package 学生管理系统;

import java.util.ArrayList;
import java.util.Scanner;

public class StudentSystem {

    public static void operate() {
        Scanner sc1 = new Scanner(System.in);
        ArrayList<Student> StudentList = new ArrayList<Student>();


        while (true) {
            System.out.println("-------------欢迎来到学生管理系统----------------");
            System.out.println("1：添加学生");
            System.out.println("2：删除学生");
            System.out.println("3：修改学生");
            System.out.println("4：查询学生");
            System.out.println("5：退出");
            System.out.println("请输入您的选择:");
            String choose = sc1.next();
            switch (choose) {
                case "1" -> pushStudent(StudentList);
                case "2" -> DeleteStudent(StudentList);
                case "3" -> ediotStudent(StudentList);
                case "4" -> queryStudent(StudentList);
                case "5" -> System.exit(0);
                default -> System.out.println("输入错误,请重新输入");
            }
        }


    }

    public static void pushStudent(ArrayList<Student> list1) {
        Scanner sc1 = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入你要添加新学生的ID");
            id = sc1.next();
            if (contains(list1, id)) {
                System.out.println("id存在,请您重新输入");
            } else {
                break;
            }
        }
        System.out.println("请输入你要添加新学生的姓名");
        String name = sc1.next();

        System.out.println("请输入你要添加新学生的年龄");
        int age = sc1.nextInt();

        System.out.println("请输入你要添加新学生的地址");
        String address = sc1.next();

        Student s1 = new Student(id, name, age, address);
        list1.add(s1);
        System.out.println("新学生,添加成功!");
    }

    public static void DeleteStudent(ArrayList<Student> list1) {
        Scanner sc1 = new Scanner(System.in);
        System.out.println("请输入你要删除学生的id");
        String id = sc1.next();
        int index = getIndex(list1, id);
        if (index < 0) {
            System.out.println("你输入的id号不存在,请重新输入");
            return;
        }

        list1.remove(index);
        System.out.println("id号为" + id + "学生已被删除");
    }

    public static void ediotStudent(ArrayList<Student> list1) {
        System.out.println("请输入要修改的id号");
        Scanner sc1 = new Scanner(System.in);
        String id = sc1.next();
        int index = getIndex(list1, id);
        if (index < 0) {
            System.out.println("你输入的id号不存在,请重新输入");
            return;
        }
        while (true) {
            System.out.println("请输入你要修改新学生的id");
            id = sc1.next();
            if (contains(list1, id)) {
                System.out.println("id存在,请您重新输入");
            } else {
                list1.get(index).setId(id);
                break;
            }
        }
        System.out.println("请输入你要修改新学生的姓名");
        String name = sc1.next();
        list1.get(index).setName(name);


        System.out.println("请输入你要修改新学生的年龄");
        int age = sc1.nextInt();
        list1.get(index).setAge(age);

        System.out.println("请输入你要修改新学生的地址");
        String address = sc1.next();
        list1.get(index).setAddress(address);
    }

    public static void queryStudent(ArrayList<Student> list1) {
        if (list1.size() == 0) {
            System.out.println("当前无学生信息，请添加后再查询");
            return;
        }
        System.out.println("\t id \t 姓名 \t 年龄 \t家庭住址");
        for (int i = 0; i < list1.size(); i++) {
            System.out.println("\t" + list1.get(i).getId() +
                    "\t" + "\t" + list1.get(i).getName() +
                    "\t" + list1.get(i).getAge() +
                    "\t" + list1.get(i).getAddress());
        }
    }

    //index方法
    public static int getIndex(ArrayList<Student> list1, String id) {
        for (int i = 0; i < list1.size(); i++) {
            if (list1.get(i).getId().equals(id)) {
                return i;
            }
        }
        return -1;
    }

    //包含方法
    public static boolean contains(ArrayList<Student> list1, String id) {
        return getIndex(list1, id) >= 0;
    }


}
